#!/bin/bash
#
# Highlight changes between pf servers
#
# Copyright (C) 2005-2019 Inverse inc.
#
# Author: Inverse inc. <info@inverse.ca>
#
# Licensed under the GPL
#

# compares the whole server
#find /etc/ /usr/local/pf /var/named -regextype posix-egrep -type f \! -regex "(/usr/local/pf/logs/.+)|(/usr/local/pf/var/.+)|(/etc/adjtime)|(/etc/aliases.db)|(/etc/blkid/.+)|(/etc/gconf/.+)|(/etc/group-?)|(/etc/gshadow-?)|(/etc/gtk-2.0/.+)|(/etc/hosts)|(/etc/ld.so.cache)|(/etc/lvm/.cache)|(/etc/mail/.+)|(/etc/pam.d/.+)|(/etc/pango/.+)|(/etc/pki/.+)|(/etc/ppp/.+)|(/etc/prelink.cache)|(/etc/resolv.conf)|(/etc/rndc.key)|(/etc/security/console.apps/.+)|(/etc/shadow-?)|(/etc/ssh/.+)|(/etc/sysconfig/hwconf)|(/etc/sysconfig/network.*)|(/etc/sysconfig/rhn/.+)|(/etc/udev/.+)|(/etc/X11/.+)|(/usr/local/pf/html/admin/tmp/.+)" | sort | xargs md5sum > ~/md5_local.txt && ssh $1 'find /etc/ /usr/local/pf /var/named -regextype posix-egrep -type f \! -regex "(/usr/local/pf/logs/.+)|(/usr/local/pf/var/.+)|(/etc/adjtime)|(/etc/aliases.db)|(/etc/blkid/.+)|(/etc/gconf/.+)|(/etc/group-?)|(/etc/gshadow-?)|(/etc/gtk-2.0/.+)|(/etc/hosts)|(/etc/ld.so.cache)|(/etc/lvm/.cache)|(/etc/mail/.+)|(/etc/pam.d/.+)|(/etc/pango/.+)|(/etc/pki/.+)|(/etc/ppp/.+)|(/etc/prelink.cache)|(/etc/resolv.conf)|(/etc/rndc.key)|(/etc/security/console.apps/.+)|(/etc/shadow-?)|(/etc/ssh/.+)|(/etc/sysconfig/hwconf)|(/etc/sysconfig/network.*)|(/etc/sysconfig/rhn/.+)|(/etc/udev/.+)|(/etc/X11/.+)|(/usr/local/pf/html/admin/tmp/.+)" | sort | xargs md5sum' > ~/md5_remote.txt && diff -u ~/md5_local.txt ~/md5_remote.txt

# compares only pf-related files
# TODO use temporary files instead of writing to user's home
# TODO pf.conf should always be different on local interface IP (doublecheck that we can't put virtual-ip maybe?)

if [[ -z "$1" ]]; then
        echo "$0: diff-tool for packetfence files"
        echo "Compares all packetfence-related files with the same ones on another server"
        echo
        echo "Usage is: pf-sync [user@]host"
        exit 0
else
        find /usr/local/pf -regextype posix-egrep -type f \! -regex "(/usr/local/pf/logs/.+)|(/usr/local/pf/var/.+)|(/usr/local/pf/html/admin/tmp/.+)" | sort | xargs md5sum > ~/md5_local.txt && ssh $1 'find /usr/local/pf -regextype posix-egrep -type f \! -regex "(/usr/local/pf/logs/.+)|(/usr/local/pf/var/.+)|(/usr/local/pf/html/admin/tmp/.+)" | sort | xargs md5sum' > ~/md5_remote.txt && diff -u ~/md5_local.txt ~/md5_remote.txt

fi
